import {
  ajaxFun
} from "../../utils/ajax"
import Notify from '@vant/weapp/notify/notify';
Page({
  data: {
    list: [],
    page: 1,
    limit: 20,
    isValid: true,
    total: 0, //购物数量 3
    // allicon: true, //全选没用使用
    sum: 0, //总价
    num: 0,
    text:"管理"
  },

  onShow() {
    this.getGata()
  },
  async getGata() {
    let {
      list,
      total
    } = await ajaxFun("/cart/list", "get", {
      page: this.data.page,
      limit: this.data.limit,
      isValid: this.data.isValid
    })



    this.setData({
      list,
      total
    }, () => {
      
      this.computedPrice()
    })
  },
  computedPrice() { //计算总价
    let sum = 0; //注意从0*****重新计算
    let num = 0; //注意从0*****重新计算
    let list = this.data.list
    for (let i = 0; i < list.length; i++) {
      if (list[i].attrStatus) {
        sum += list[i].vipPrice * list[i].cartNum //总价累加
        num++ //数量累加
      }
    }
    this.setData({
      sum,
      num
    })
  },





  allchoose() { // 全选/反选
    let list = this.data.list;
    let num=this.data.total == this.data.num?0:this.data.total;//三元运算
    let blo=this.data.total == this.data.num?false:true;//三元运算
    list.map(item=>item.attrStatus=blo)

    this.setData({
      list,
      num
    },()=>{
      this.computedPrice()
    })
  },





  singleIcon(e) { // 单机单个商品
    let i = e.target.dataset.index;//获取下标
    let list = this.data.list;
    list[i].attrStatus = !list[i].attrStatus;//取反
  
    this.setData({
      list,
    }, () => {
      this.computedPrice()
    })
  },





  async onChange(e) {
 
    let i = e.target.dataset.index
    let res = await ajaxFun("/cart/num", "post", {
      id: e.target.dataset.id,
      number: e.detail
    },"application/x-www-form-urlencoded")
    Notify('数量修改成功');

    let list = this.data.list;
    list[i].cartNum = e.detail;
    this.setData({
      list
    }, () => {
      this.computedPrice()

    })



  },
  changText(){
     this.setData({
       text:this.data.text=="管理"?"取消":"管理"
     })
  },
 async collectionFun(){//收藏事件
    let list=this.data.list;
    let arry=[]
    list.map(item=>{
      if(item.attrStatus){
        arry.push(item.productId)
      }
    })
    console.log(arry);
    let res=await ajaxFun("/collect/all","post",{
      category:"product",
      id:arry
    })
    Notify('搜藏成功');
  },


  
  async delFun(){
    let list=this.data.list;
    let arry=[]
    list.map(item=>{
      if(item.attrStatus){
        arry.push(item.id)
      }
    })
    console.log(arry);
    let res=await ajaxFun("/cart/delete","post",{
      ids:arry
    },"application/x-www-form-urlencoded")
    this.getGata()
  },

async  onClickButton(){//去支付页面
      let arry=[];
      this.data.list.map(item=>{
           arry.push({
            shoppingCartId:item.id
           })
      })

    let {preOrderNo} = await ajaxFun("/order/pre/order", "post", {
      orderDetails:arry,
      preOrderType: "shoppingCart"
    })
    if(preOrderNo){
      wx.navigateTo({//去支付页面
      url: `/indexPackage/pages/payfor/payfor?preOrderNo=${preOrderNo}`,
    })
    }
    
  }
})